<?php


namespace App\Http\Controllers;

use App\Models\Branch;
use App\Models\User;
use App\Models\Worker;
use App\Models\WorkerBranch;

class BranchUtils
{
    // 查找部门部门的所有上级部门列表
    public static function getParentBranchs($branch_id, $branchs_dics, $parent_branchs)
    {
        foreach ($branchs_dics as $branch) {
            if ($branch['id'] == $branch_id) {
                if ($branch['pid'] != 0) {
                    $parent_branchs[] = $branch['pid'];
                    return self::getParentBranchs($branch['pid'], $branchs_dics, $parent_branchs);
                } else {
                    return $parent_branchs;
                }
            }
        }
        return $parent_branchs;
    }

    // 查找所有部门并且按照上级部门整理好array
    public static function getAllBranchs()
    {
        $branchs = Branch::orderBy('branch.pid', 'asc')->get();
        $branchs_dics = [];
        $pid_list = [];
        foreach ($branchs as $branch) {
            $branch_dic = [];
            $branch_dic['id'] = $branch->id;
            $branch_dic['name'] = $branch->name;
            $branch_dic['pid'] = $branch->pid;
            $branch_dic['hid'] = $branch->hid;
            if (empty($branchs_dics[$branch->pid])) {
                $new_list = [];
                foreach ($pid_list as $pid_dic) {
                    if ($pid_dic['hid'] == null) {
                        $top_id = $pid_dic['id'];
                        $new_list[] = $pid_dic;
                        $last_pid = $pid_list[0]['pid'];
                        $branchs_dics[$last_pid] = self::getArrayHid($new_list, $pid_list, $top_id);
                        break;
                    }
                }
                $pid_list = [];
            }
            $pid_list[] = $branch_dic;
            $branchs_dics[$branch->pid] = $pid_list;
        }
        return $branchs_dics;
    }

    // 将小数组按照hid排序
    public static function getArrayHid($new_list, $pid_list, $id)
    {
        foreach ($pid_list as $pid_dic) {
            if ($pid_dic['hid'] == $id) {
                $new_list[] = $pid_dic;
                $pid_id = $pid_dic['id'];
                return self::getArrayHid($new_list, $pid_list, $pid_id);
            }
        }
        return $new_list;
    }

    // 查找单个部门子部门
    public static function getChildBranchs($branch_id, $branchs_dics, $child_branchs)
    {
        if (isset($branchs_dics[$branch_id])) {
            foreach ($branchs_dics[$branch_id] as $branch) {
                $child_branchs[] = $branch['id'];
                $child_branchs = self::getChildBranchs($branch['id'], $branchs_dics, $child_branchs);
            }
        }
        return $child_branchs;
    }

    // 传入部门id，返回由上级至子级的部门树
    public static function getBranchChain($branch_id)
    {
        $chain_branchs = self::getChildBranchs($branch_id, self::getAllBranchs(), [$branch_id]);
        $branchs = Branch::all();
        return self::getParentBranchs($branch_id, $branchs, $chain_branchs);
    }

    // 得到用户所在部门id
    public static function get_user_branch($user_id)
    {
        $user = User::find($user_id);
        if (empty($user->branch_id)) {
            if (empty($user->id_number) && empty($user->worker_id)) {
                return ['code' => 300, 'msg' => '该用户无信息，无法提供服务'];
            } else {
                $users = User::where('id_number', $user->id_number)->get();
                $branch_id = null;
                foreach ($users as $this_user) {
                    if ($this_user->branch_id != null) {
                        $branch_id = $this_user->branch_id;
                        break;
                    }
                }
            }
        } else {
            $branch_id = $user->branch_id;
        }
        if ($branch_id == null) {
            if ($user->worker_id != null) {
                $worker = Worker::where('id', $user->worker_id)->with('branches')->first();
                if ($worker) {
                    if (isset($worker['branches'])) {
                        $branch_id = $worker['branches'][0]['id'];
                    } else {
                        return ['code' => 300, 'msg' => '该用户无部门信息，无法提供服务'];
                    }
                }
            } else {
                return ['code' => 300, 'msg' => '该用户无信息，无法提供服务'];
            }
        }
        return ['code' => 200, 'msg' => $branch_id];
    }

    // 传入worker_id，返回由上级至子级的部门树
    public static function get_worker_branch_list($worker_id)
    {
        $worker_branch = WorkerBranch::where('worker_id', $worker_id)->first();
        if ($worker_branch) {
            $branch_id = $worker_branch->branch_id;
            return self::getBranchChain($branch_id);
        } else {
            return [];
        }
    }

    // 根据传入部门查找到是国逸，安盛还是逸盛(国邦盛世专用)
    public static function selePbranch($branch_id)
    {
        $branchs = Branch::select(
            'branch.id',
            'branch.pid'
        )->orderBy('branch.id.desc');
        $k = 0;
        while ($k < 8) {
            $k = $k + 1;
            foreach ($branchs as $branch) {
                if ($branch->id == $branch_id) {
                    if ($branch->pid == 1) {
                        return $branch_id;
                    } else {
                        $branch_id = $branch->pid;
                        break;
                    }
                }
            }
        }
        return 1;
    }
}
